/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.jakubiak.jpa.translator.example.jsf;

import eu.jakubiak.jpa.translator.Translator;
import eu.jakubiak.jpa.translator.example.jsf.entities.Hotel;
import eu.jakubiak.jpa.translator.example.jsf.utils.PersistenceUtil;
import eu.jakubiak.jpa.translator.locale.LocaleParser;
import java.util.List;
import java.util.Locale;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author jakubiak_a
 */
public class HotelManager {
    
    private static final Log log = LogFactory.getLog(HotelManager.class);

    private List<Hotel> hotels;
    private Hotel hotel;
    private Locale locale;
    

    public HotelManager() {
        showList();
    }

    public String showList() {
        EntityManager em = PersistenceUtil.createEntityManager();
        hotels = em.createQuery("select h from Hotel h order by h.id").getResultList();
        em.close();
        return "list";
    }

    public String add() {
        log.trace("add");
        hotel = new Hotel();
        locale = null;
        return "edit";
    }

    public String edit() {
        log.trace("edit");
        hotel = getHotelFromRequest();
        locale = null;
        return "edit";
    }

    public String editLocale() {
        log.trace("edit Locale");
        locale = getLocaleFromRequest();
        hotel = new Translator(locale).translate(Hotel.class, getHotelFromRequest());
        log.trace("edit Locale " + locale + " " + hotel);
        return "edit";
    }
    
    

    public String save() {
        EntityManager em = PersistenceUtil.createEntityManager();
        em.getTransaction().begin();
        if (hotel.getId() == null) {
            em.persist(hotel);
        } else {
            if (Translator.isTranslated(hotel)) {
                log.trace("megre translated " + Translator.getDelegate(hotel));
                em.merge(Translator.getDelegate(hotel));
            } else {
                log.trace("megre " + hotel);
                hotel = em.merge(hotel);
            }
        }
        em.getTransaction().commit();
        em.clear();
        hotels = em.createQuery("select h from Hotel h order by h.id").getResultList();
        em.close();
        return "list";
    }

    public List<Hotel> getHotels() {
        return hotels;
    }

    public Hotel getHotel() {
        return hotel;
    }

    public Locale getLocale() {
        return locale;
    }

    
    
    

    private Long getHotelIdFromRequest() {
        String hotelId = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("hotelId");
        return Long.parseLong(hotelId);
    }

    private Hotel getHotelFromRequest() {
        Long hotelId = getHotelIdFromRequest();
        for (Hotel h : getHotels()) {
            if (h.getId().equals(hotelId)) {
                return h;
            }
        }
        throw new NullPointerException("Hotel not found");
    }
    
    private Locale getLocaleFromRequest() {
        String localeString = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("locale");
        return new LocaleParser(localeString).getLocale();
    }
}
